package io.renren.modules.simida.service.impl;

import io.renren.common.utils.Constant;
import io.renren.datasource.annotation.DataSource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.simida.dao.VersionDao;
import io.renren.modules.simida.entity.VersionEntity;
import io.renren.modules.simida.service.VersionService;


@Service("versionService")
public class VersionServiceImpl extends ServiceImpl<VersionDao, VersionEntity> implements VersionService {

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VersionEntity> page = this.page(
                new Query<VersionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public VersionEntity getNewestAndroid() {
        QueryWrapper<VersionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        return super.getOne(queryWrapper);
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public void add(VersionEntity version) {
        version.setCreateTime(new Date());
        version.setUpdateTime(new Date());
        super.save(version);
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public void updateVersion(VersionEntity version) {
        version.setUpdateTime(new Date());
        super.updateById(version);
    }


}